package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.StudentAtSchoolScoreDivide;
import com.xmy.cultivate.service.IStudentAtSchoolScoreDivideService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 学生所在学校分数划分 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-04-25
 */
@RestController
@RequestMapping("/api/studentAtSchoolScoreDivide")
@Api("学生所在学校分数划分控制器")
public class StudentAtSchoolScoreDivideController {

    @Autowired
    IStudentAtSchoolScoreDivideService studentAtSchoolScoreDivideService;

    @Autowired
    IdWorker idWorker;

    @GetMapping("/newStudentScore")
    @RequestBody
    @ApiOperation("学校分数划分新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student_at_school_id" , value = "学校id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "grade_section" , value = "学段" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "score" , value = "分数或者分段" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "score_type" , value = "分数类型（1分数，2分段）" ,required = true , dataType = "String"),
            @ApiImplicitParam(name = "score_start" , value = "当前分段开始分数" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "score_end" , value = "当前分段结束分数" , required = false , dataType = "String")
    })
    public Result newStudentScore(@RequestParam("student_at_school_id") String studentAtSchoolId,
                                  @RequestParam("grade_section") String gradeSection,
                                  @RequestParam("score") String score,
                                  @RequestParam("score_type") String scoreType,
                                  @RequestParam("score_start") BigDecimal scoreStart,
                                  @RequestParam("score_end") BigDecimal scoreeEnd){
        StudentAtSchoolScoreDivide schoolScore = new StudentAtSchoolScoreDivide();
        schoolScore.setId(idWorker.nextId());
        schoolScore.setStudentAtSchoolId(Long.valueOf(studentAtSchoolId));
        schoolScore.setGradeSection(Integer.valueOf(gradeSection));
        schoolScore.setScore(score);
        schoolScore.setScoreType(Integer.valueOf(scoreType));
        schoolScore.setScoreStart(scoreStart);
        schoolScore.setScoreEnd(scoreeEnd);
        studentAtSchoolScoreDivideService.save(schoolScore);
        return new Result(Code.OK,null,"学校分数划分生成成功~");
    }

    @GetMapping("/allStudentScore")
    @ApiOperation("分段查询学校分段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student_at_school_id" , value = "学校id" , required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum" , value = "分页数" , required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸" , required = true, dataType = "String"),
    })
    @RequestBody
    public Result allStudentScore(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        String studentAtSchoolId = (String) reMap.get("student_at_school_id");
        IPage<StudentAtSchoolScoreDivide> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(studentAtSchoolId) , "student_at_school_id",studentAtSchoolId);
        studentAtSchoolScoreDivideService.allStudentScore(iPage,queryWrapper);
        return new Result(Code.OK,iPage,"学校分段信息查询完成");
    }

    @GetMapping("/updateStudentscore")
    @ApiOperation("学校分段信息更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "学校分数分段信息id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "student_at_school_id" , value = "学校id" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "grade_section" , value = "学段" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "score" , value = "分数或者分段" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "score_type" , value = "分数类型（1分数，2分段）" ,required = false , dataType = "String"),
            @ApiImplicitParam(name = "score_start" , value = "当前分段开始分数" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "score_end" , value = "当前分段结束分数" , required = false , dataType = "String")
    })
    public Result updateStudentscore(@RequestParam("id") String id,
                                     @RequestParam("student_at_school_id") String studentAtSchoolId,
                                     @RequestParam("grade_section") String gradeSection,
                                     @RequestParam("score") String score,
                                     @RequestParam("score_type") String scoreType,
                                     @RequestParam("score_start") BigDecimal scoreStart,
                                     @RequestParam("score_end") BigDecimal scoreeEnd){
        StudentAtSchoolScoreDivide schoolScore = studentAtSchoolScoreDivideService.getById(id);
        if(!Objects.equals(studentAtSchoolId,null)){
            schoolScore.setStudentAtSchoolId(Long.valueOf(studentAtSchoolId));
        }
        if(!Objects.equals(gradeSection,null)){
            schoolScore.setGradeSection(Integer.valueOf(gradeSection));
        }
        if(!Objects.equals(score,null)){
            schoolScore.setScore(score);
        }
        if (!Objects.equals(scoreType,null)){
            schoolScore.setScoreType(Integer.valueOf(scoreType));
        }
        if (!Objects.equals(scoreStart,null)){
            schoolScore.setScoreStart(scoreStart);
        }
        if (!Objects.equals(scoreeEnd,null)){
            schoolScore.setScoreEnd(scoreeEnd);
        }
        studentAtSchoolScoreDivideService.updateById(schoolScore);
        return new Result(Code.OK,null,"学校分段信息修改完成~");
    }
    @GetMapping("/deleteStudentScore")
    @ApiOperation("学校分段信息删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "学校分数分段信息id" , required = true , dataType = "String"),
    })
    public Result deleteStudentScore(@RequestParam String id){
        studentAtSchoolScoreDivideService.removeById(id);
        return new Result(Code.OK,null,"学校分段信息删除成功~");
    }
}
